<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


$packageid = (int) get('package');
$versionid = (int) get('version');

$pData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_packages WHERE id = '{$packageid}'");
$vData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE id = '{$versionid}'");



$vSelect = '<select id="vms-version-select"><option value="0">Select package first</option>';
$pSelect = '<select id="vms-package-select"><option value="0">Choose a package...</option>';

$packages = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_packages ORDER BY keyname ASC");
foreach($packages as $k=>$v){
    if($v['id'] == $packageid) $selected = 'selected="selected"';
    else $selected = '';
    $pSelect .= '<option value="'.$v['id'].'" '.$selected.'>'.$this->getContentLang('package_title', $v['id']).'</option>';
}

$versions = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE package_id = '{$packageid}' ORDER BY version DESC");

if(is_array($versions) && count($versions)>0){
    foreach($versions as $k=>$v){
        if($v['id'] == $versionid) $selected = 'selected="selected"';
        else $selected = '';
        $vSelect .= '<option value="'.$v['id'].'" '.$selected.'>'.$v['version'].'</option>';
    }
}

$vSelect .= '</select>';
$pSelect .= '</select>';

echo $vSelect.$pSelect;

echo '<div style="clear: both"></div>';

$js = <<<JS
$(document).ready(function(){
    $('#vms-package-select').change(function(){
        var vsel = $('#vms-version-select').get(0);
        var versionid = vsel.options[vsel.selectedIndex].value;
        var packageid = this.options[this.selectedIndex].value;
        window.location = '{$this->adminUrl}/publish/?package='+packageid+'&version='+versionid;
    });
    $('#vms-version-select').change(function(){
        var psel = $('#vms-package-select').get(0);
        var packageid = psel.options[psel.selectedIndex].value;
        var versionid = this.options[this.selectedIndex].value;
        window.location = '{$this->adminUrl}/publish/?package='+packageid+'&version='+versionid;
    });
});
JS;


$VIEW->addJs($js);








if(!is_array($pData) || count($pData)==0 || !is_array($vData) || count($vData)==0){
    // need to select a package and version
    return;
}














if(Antz_IntelliForm::submitted('publish-version')){
    $groups = $_POST['groups'];
    if(!is_array($groups)){
        JFX::addError('No groups selected');
    }else{
        $DB->delete($CONFIG->dbprefix.'vms_versions_to_groups', "package_id = '{$packageid}' AND version_id = '{$versionid}'");
        foreach($groups as $groupkey){
            $dbParams = array(
                'version_id' => $versionid,
                'package_id' => $packageid,
                'group_key' => $groupkey
            );
            $DB->insert($CONFIG->dbprefix.'vms_versions_to_groups', $dbParams);
        }
        JFX::addSuccess('Permissions updated');
    }
}



















$groups = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_groups ORDER BY keyname ASC");

$groupOpts = array();
foreach($groups as $k=>$v){
    $groupOpts[$v['keyname']] = $v['title'];
}

$selectedGroups = array();
$selectedGroupsTemp = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_versions_to_groups WHERE version_id = '{$versionid}'");
foreach($selectedGroupsTemp as $k=>$v){
    $selectedGroups[] = $v['group_key'];
}

$formFields = array(
    array(
        'type' => 'select',
        'multiple' => true,
        'name' => 'groups[]',
        'options' => $groupOpts,
        'value' => $selectedGroups,
        'size' => '6',
        'label' => 'Group Access'
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'label' => '&nbsp;',
        'value' => 'Submit'
    )
);


echo JFX::makeRapidForm($formFields, 'Access Details', 'publish-version');

























